﻿import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class CourseDao {

	public static ArrayList<Course> queryCourseList() {
		 ArrayList<Course> courseList = new ArrayList<>();
		/**
		 * 查询学生列表
		 */
			Connection conn = ConnectionUtil.getConnection();
	        Statement stmt = null;
	        try{
	           
	            // 执行查询
	            System.out.println(" 实例化Statement对象...");
	            stmt = conn.createStatement();
	            String sql;
	            
	            sql = "SELECT id, kcm, ls, zy, cj, kkxq FROM course join score "+ "on course.kch=score.kch";
	            ResultSet rs =  stmt.executeQuery(sql);
	        
	            // 展开结果集数据库
	            while(rs.next()){
	                // 通过字段检索
	                int id  = rs.getInt("id");
	                String kcm = rs.getString("kcm");
	                String ls = rs.getString("ls"); 
	                String zy=rs.getString("zy");
	                int cj = rs.getInt("cj");
	                int kkxq = rs.getInt("kkxq");
	                
	                Course course = new Course(id, kcm, ls, zy, cj, kkxq);
					courseList.add(course);
	            }
	     /*//完成后关闭
            rs.close();
            stmt.close();*/
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }/*finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }*/
	        System.out.println("查询成功！");
	        return courseList;
	
	}
}
